from utils.sendmethod import Send_Method
from utils.getkeyword import GetKeyWord
from utils.use_mysql import use_mysql
import pytest
import json
class OmsPortalOrderController(Send_Method):
    all_url='http://47.108.206.100:8085'
    def generateConfirmOrder(self,*orderIds,headers):      # 根据购物车信息生成确认单
        url=self.all_url+'/order/generateConfirmOrder'
        payload = list(orderIds)
        print(payload)
        res = self.send_Method(method='post',url=url,json=payload,headers=headers)
        return res
    def generateOrder(self,*cartIds,memberReceiveAddressId,payType,headers):             # 根据购物车信息生成订单
        url = self.all_url + '/order/generateOrder'
        payload ={"cartIds": [list(cartIds)],
                  "memberReceiveAddressId": memberReceiveAddressId,
                  "payType": payType}
        res = self.send_Method(method='post', url=url, json=payload, headers=headers)
        return res
    def detail(self,orderId,headers):            # 根据ID获取订单详情
        url = self.all_url + f'/order/detail/{orderId}'
        payload = {'orderId':orderId}
        res = self.send_Method(method='get', url=url, params=payload, headers=headers)
        return res
    def list(self,pageNum,pageSize,status,headers):                 # 按状态分页获取用户订单列表
        url=self.all_url + '/order/list'
        payload = {'pageNum':pageNum,'pageSize':pageSize,'status':status}
        res = self.send_Method(method='get', url=url, params=payload, headers=headers)
        return res
    def paySuccess(self,orderId,payType,headers):                   #用户支付成功的回调
        url=self.all_url + '/order/paySuccess'
        payload={'orderId':orderId,'payType':payType}
        res = self.send_Method(method='post',url=url,data=payload,headers=headers)
        return res
    def deleteOrder(self,orderId,headers):                   #用户删除订单
        url = self.all_url + '/order/deleteOrder'
        payload = {'orderId': orderId}
        res = self.send_Method(method='post', url=url, data=payload, headers=headers)
        return res
    def confirmReceiveOrder(self,orderId,headers):           #用户确认收货
        url = self.all_url + '/order/confirmReceiveOrder'
        payload = {'orderId': orderId}
        res = self.send_Method(method='post', url=url, data=payload, headers=headers)
        return res
    def cart_add(self,num,headers,month='05',day=30):           #加入购物车
        url = self.all_url + '/cart/add'
        payload1 = {
            "createDate": f"2023-{month}-{day}T01:16:56.435Z",
            "deleteStatus": 0,
            "memberNickname": " ",
            "modifyDate": f"2023-{month}-{day}T01:16:56.435Z",
            "price": 3999.00,
            "productAttr": "华为 HUAWEI P20",
            "productBrand": "华为",
            "productCategoryId": 19,
            "productId": 26,
            "productName": f"好东西{num}",
            "productPic": f"订单{num}",
            "productSkuCode": "201806070026004",
            "productSkuId": 113,
            "productSn": "6946605",
            "productSubTitle": "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待",
            "quantity": 1
        }
        payload2={
            "createDate": f"2023-{month}-{day}T01:16:56.435Z",
            "deleteStatus": 0,
            "memberNickname": " ",
            "modifyDate": f"2023-{month}-{day}T01:16:56.435Z",
            "price": 699.00,
            "productAttr": "小米 红米5A 全网通版 3GB+32GB 香槟金 移动联通电信4G手机 双卡双待",
            "productBrand": "小米",
            "productCategoryId": 19,
            "productId": 28,
            "productName": f"好东西{num}",
            "productPic": f"订单{num}",
            "productSkuCode": "201808270028004",
            "productSkuId": 105,
            "productSn": "7437789",
            "productSubTitle": "8天超长待机，137g轻巧机身，高通骁龙处理器小米6X低至1299，点击抢购",
            "quantity": 1
        }
        if num==1:
            res = self.send_Method(method='post', url=url, json=payload1, headers=headers)
        if num==2:
            res = self.send_Method(method='post', url=url, json=payload2, headers=headers)
    @staticmethod                     #获取订单号
    def orderIds():
        sql = "SELECT * FROM oms_order where member_username='zdm2'"
        id = use_mysql(sql)
        order_Ids = [id[i][0] for i in range(len(id))]
        return order_Ids
    @staticmethod
    def order_update_delivery():               #发货
        login_url='http://47.108.206.100:8080/admin/login'
        payload1={"username":"admin","password":"macro123"}
        res = Send_Method.send_Method(method='post',url=login_url,json=payload1)
        get_token = GetKeyWord.getKeyWord(res.json(), 'token')
        get_tokenHead = GetKeyWord.getKeyWord(res.json(), 'tokenHead')
        get_headers = {'Authorization': get_tokenHead + get_token}
        oud_url='http://47.108.206.100:8080/order/update/delivery'
        payload2=[{"orderId": OmsPortalOrderController().orderIds()[0]}]
        Send_Method.send_Method(method='post',url=oud_url,json=payload2,headers=get_headers)

    @staticmethod
    def cartIds(month='05',day=29):        #获取商品号
        sql1="select id FROM ums_member where username='zdm2'"
        memberid=use_mysql(sql1)[0][0]
        sql2=f'select id from oms_cart_item where modify_date like "2023-{month}-{day}%" and member_id={memberid};'
        id=use_mysql(sql2)
        ids=[id[i][0] for i in range(len(id))]       #获取商品id:self.test_cart_add()
        return ids


if __name__ == '__main__':
    # login_url = 'http://47.108.206.100:8080/admin/login'
    # payload1 = {"username": "admin", "password": "macro123"}
    # res = Send_Method.send_Method(method='post', url=login_url, json=payload1)
    # get_token = GetKeyWord.getKeyWord(res.json(), 'token')
    # get_tokenHead = GetKeyWord.getKeyWord(res.json(), 'tokenHead')
    # get_headers = {'Authorization': get_tokenHead + get_token}
    # oud_url = 'http://47.108.206.100:8080/order/update/delivery'
    # payload2 = [{"orderId": OmsPortalOrderController().orderIds()[5]}]
    # res=Send_Method.send_Method(method='post', url=oud_url, json=payload2, headers=get_headers)
    # print(res.json())
    url = 'http://47.108.206.100:8085/sso/login'
    payload = {'password': 123456, 'username': 'zdm2'}
    res = Send_Method.send_Method(method='post', url=url, data=payload)
    get_token = GetKeyWord.getKeyWord(res.json(), 'token')
    get_tokenHead = GetKeyWord.getKeyWord(res.json(), 'tokenHead')
    headers = {'Authorization': get_tokenHead + get_token}
    print(res.json())
    print(headers)
    o = OmsPortalOrderController()
    res=o.generateConfirmOrder(8621,headers=headers).json()
    print(res)

